package com.lupengning.payment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.lupengning.payment.entity.OrderInfo;
import com.lupengning.payment.entity.RefundInfo;
import com.lupengning.payment.enums.wxpay.WxRefundStatus;
import com.lupengning.payment.mapper.RefundInfoMapper;
import com.lupengning.payment.service.OrderInfoService;
import com.lupengning.payment.service.RefundInfoService;
import com.lupengning.payment.util.OrderNoUtils;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author lupengning
 * @version 1.0
 * @description:
 * @date 2022/1/16 1:06
 */
@Service
@Slf4j
public class RefundsInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements
    RefundInfoService {

  @Resource
  private OrderInfoService orderInfoService;

  /**
   * 根据订单号创建退款订单
   *
   * @param orderNo
   * @return
   */
  @Override
  public RefundInfo createRefundByOrderNo(String orderNo, String reason) {
    log.info("createRefundByOrderNo=================>orderNo={},reason={}", orderNo, reason);
    //根据订单号获取订单信息
    OrderInfo orderInfo = orderInfoService.getOrderByOrderNo(orderNo);
    log.info("要退款的订单=========>{}",new Gson().toJson(orderInfo));
    //根据订单号生成退款订单
    if (null == orderInfo) {
      return null;
    }
    RefundInfo refundInfo = new RefundInfo();
    refundInfo.setOrderNo(orderNo);
    refundInfo.setRefundNo(OrderNoUtils.getRefundNo());
    refundInfo.setTotalFee(orderInfo.getTotalFee());
    refundInfo.setRefund(orderInfo.getTotalFee());
    refundInfo.setReason(reason);
    //保存退款订单
    baseMapper.insert(refundInfo);
    return refundInfo;
  }

  /**
   * 记录退款记录
   *
   * @param content
   */
  @Override
  public void updateRefund(String content) {
    //将json字符串转换成Map
    Gson gson = new Gson();
    Map<String, String> resultMap = gson.fromJson(content, HashMap.class);
    //根据退款单编号修改退款单
    QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("refund_no", resultMap.get("out_refund_no"));
    //设置要修改的字段
    RefundInfo refundInfo = new RefundInfo();
    //微信支付退款单号
    refundInfo.setRefundId(resultMap.get("refund_id"));
    //查询退款和申请退款中的返回参数
    if (resultMap.get("status") != null) {
      //退款状态
      refundInfo.setRefundStatus(resultMap.get("status"));
      //将全部响应结果存入数据库的content字段
      refundInfo.setContentReturn(content);
    }
    //退款回调中的回调参数
    if (resultMap.get("refund_status") != null) {
      //退款状态
      refundInfo.setRefundStatus(resultMap.get("refund_status"));
      //将全部响应结果存入数据库的content字段
      refundInfo.setContentNotify(content);
    }
    //更新退款单
    baseMapper.update(refundInfo, queryWrapper);
  }

  /**
   * 找出申请退款超过minutes分钟并且未成功的退款单
   *
   * @param minutes
   * @return
   */

  @Override
  public List<RefundInfo> getNoRefundOrderByDuration(int minutes) {
    //minutes分钟之前的时间
    Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
    QueryWrapper<RefundInfo> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("refund_status", WxRefundStatus.PROCESSING.getType());
    queryWrapper.le("create_time", instant);
    List<RefundInfo> refundInfoList = baseMapper.selectList(queryWrapper);
    return refundInfoList;
  }
}
